package com.czp.ssm.service.impl;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.czp.ssm.bean.Hotel;
import com.czp.ssm.bean.HotelOrder;
import com.czp.ssm.bean.HotelOrderExample;
import com.czp.ssm.bean.Opera;
import com.czp.ssm.bean.OperaRule;
import com.czp.ssm.bean.PlaneOrder;
import com.czp.ssm.bean.PlaneOrderExample;
import com.czp.ssm.bean.TrainOrder;
import com.czp.ssm.bean.User;
import com.czp.ssm.dao.CompanyMapper;
import com.czp.ssm.dao.HotelMapper;
import com.czp.ssm.dao.HotelOrderMapper;
import com.czp.ssm.dao.OperaMapper;
import com.czp.ssm.dao.OperaRuleMapper;
import com.czp.ssm.dao.UserMapper;
import com.czp.ssm.service.HotelService;
import com.czp.ssm.service.OperaService;
import com.czp.ssm.utils.CommonUtils;
@Service
public class HotelServiceImpl implements HotelService {

	
	@Autowired
	private HotelMapper hotelMapper;
	@Autowired
	private OperaService operaService;
	@Autowired
	private HotelOrderMapper hotelOrderMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private OperaMapper operaMapper;
	@Autowired
	private OperaRuleMapper operaRuleMapper;
	
	public List<Hotel> findHotelListByCityNameAndLikeKey(String cityName, String key) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("CITY_NAME", cityName);
		map.put("HOTEL_NAME", key);
		System.out.println(map);
		List<Hotel> hotelList = hotelMapper.selectHotelListByCityNameAndLikeKey(map);
		return hotelList;
	}
	public void getHotelByDate(List<Hotel> hotelList, String startDate) {
		for (int i = 0; i < hotelList.size(); i++) {
			
		}
		
	}
	public Hotel findHotelByHotelId(String hotelId) {
		return hotelMapper.findHotelByHotelId(hotelId);
	}
	public int saveHotelOrder(HotelOrder hotelOrder) {
		hotelOrder.setOrderId(CommonUtils.getOrderId());
		hotelOrder.setOrderStatus("0");//待审批
		hotelOrder.setPayType("0");///为付款
		hotelOrder.setCreateOrderDate(new Date());
		//获取对应的审批人ID
		Opera opera = operaService.getOperaPersonIdByCompanyIdAndUserId(hotelOrder.getCompanyId(),hotelOrder.getUserId());
		hotelOrder.setOperaPersonId(opera.getOperaPersonId());
		int flag = hotelOrderMapper.insert(hotelOrder);
		return flag;
	}
	
	public List<HotelOrder> findAllHotelOrders() {
		HotelOrderExample example = new HotelOrderExample();
		example.setOrderByClause("ID asc");
		example.setDistinct(false);
		List<HotelOrder> hotelOrderList = hotelOrderMapper.selectByExample(example);
		for (int i = 0; i < hotelOrderList.size(); i++) {
			HotelOrder hotelOrder = hotelOrderList.get(i);
			User user = userMapper.selectUserByUserId(hotelOrder.getUserId());
			hotelOrder.setUserName(user.getUserName());
			hotelOrder.setLinkPhone(user.getUserPhone());
			Map<String, String> map = new HashMap<String, String>();
			map.put("COMPANY_ID", hotelOrder.getCompanyId());
			map.put("USER_ID", hotelOrder.getUserId());
			Opera opera = operaMapper.selectOperaPersonIdByCompanyIdAndUserId(map);
			User operaUser = userMapper.selectUserByUserId(opera.getOperaPersonId());
			hotelOrder.setOperaPersonName(operaUser.getUserName());
		}
		return hotelOrderList;
	}
	public List<HotelOrder> findHotelOrderByOperaPersonId(String operaPersonId) {
		List<HotelOrder> hotelOrders = new ArrayList<HotelOrder>();
		List<Opera> operas = operaMapper.selectOperaByOperaPersonId(operaPersonId);
		for (int i = 0; i < operas.size(); i++) {
			Opera opera = operas.get(i);
			List<HotelOrder> hotelorderList  = hotelOrderMapper.selectHotelOrderListByOpera(opera);
			if (hotelorderList != null && hotelorderList.size() > 0) {
				for (int j = 0; j < hotelorderList.size(); j++) {
					HotelOrder hotelOrder = hotelorderList.get(j);
					User user = userMapper.selectUserByUserId(opera.getUserId());
					hotelOrder.setLinkPhone(user.getUserPhone());
					OperaRule operaRule = operaRuleMapper.selectByOperaRuleId(opera.getOperaRuleId());
					hotelOrder.setOperaRuleContent(operaRule.getOperaRuleContent());
				}
				hotelOrders.addAll(hotelorderList);
			}
		}
		return hotelOrders;
	}
	public int updateHotelOrderStatus(String id, String status) {
		HotelOrder hotelOrder = new HotelOrder();
		hotelOrder.setId(Long.parseLong(id));
		hotelOrder.setOrderStatus(status);
		int flag = hotelOrderMapper.updateByPrimaryKeySelective(hotelOrder);
		return flag;
	}

}
